package com.qztc.news_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.qztc.news_end_dto.dto.Admin;
import com.qztc.news_end_dto.dto.News;
import com.qztc.news_end_dto.dto.NewsCollection;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/newscollection")
@Tag(name = "新闻收藏接口")
public class NewsCollectionController {
    @Operation(summary = "新闻收藏")
    @PostMapping("/add")
    public boolean add(@RequestBody NewsCollection newsCollection) {
        QueryWrapper<NewsCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("newsid", newsCollection.getNewsid());
        queryWrapper.eq("userid", newsCollection.getUserid());
        if(newsCollection.selectCount(queryWrapper) > 0){
            return false;
        }
        QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("newsid", newsCollection.getNewsid());
        News news1 = new News().selectOne(queryWrapper1);
        news1.setLikenum(news1.getLikenum() + 1);
        news1.updateById();
        return newsCollection.insert();

    }

    @Operation(summary = "查询指定用户的新闻收藏列表")
    @PostMapping("/getbyuserid")
    public List<News> getbyuserid(@RequestBody NewsCollection newsCollection) {
        UpdateWrapper<NewsCollection> wrapper = new UpdateWrapper<>();
        wrapper.eq("userid", newsCollection.getUserid());
        List<Integer> newsid = new ArrayList<>();
        List<NewsCollection> newsCollections = newsCollection.selectList(wrapper);
        for(NewsCollection newsCollection1 : newsCollections){
            newsid.add(newsCollection1.getNewsid());
        }
        QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
        if(newsid != null && !newsid.isEmpty()) {
            queryWrapper1.in("newsid", newsid);
        }
        else{
            return null;
        }
        return new News().selectList(queryWrapper1);
    }

    @Operation(summary = "删除用户的某个新闻")
    @DeleteMapping("/deletebyuserid_newsid")
    public boolean delete(@RequestBody NewsCollection newsCollection) {
        QueryWrapper<NewsCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", newsCollection.getUserid());
        wrapper.eq("newsid", newsCollection.getNewsid());
        if(newsCollection.selectCount(wrapper) <= 0){
            return false;
        }
            QueryWrapper<News> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("newsid", newsCollection.getNewsid());
            News news1 = new News().selectOne(queryWrapper1);
            news1.setLikenum(news1.getLikenum() - 1);
            news1.updateById();
        return newsCollection.delete(wrapper);

    }

    @Operation(summary = "获取所有新闻收藏")
    @GetMapping("/get")
    public List<NewsCollection> get() {
        return new NewsCollection().selectAll();
    }
}
